#include "common.hpp"
#include "InetAddr.hpp"
#include "Socket.hpp"
#include "TcpServer.hpp"
#include "Protocol.hpp"
#include "Netcall.hpp"

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " port" << std::endl;
}

using namespace SocketModule;

int main(int args, char* argv[])
{
    if(args != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERROR);
    }
    
    //1.创建顶层
    std::unique_ptr<Cal> cal = std::make_unique<Cal>();

    std::unique_ptr<Protocol> p = std::make_unique<Protocol>([&cal](Request& req)->Response{
        return cal->Excute(req);
    });
    
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(std::stoi(argv[1]),[&p](std::shared_ptr<Socket> &sock, InetAddr &client){
        p->GetRequest(sock, client);
    });

    tsvr->Start();
    

    return 0; 
}